package com.leoman.controller.api;

import com.leoman.core.ErrorType;
import com.leoman.core.bean.Result;
import com.leoman.entity.Circle;
import com.leoman.exception.NoDataException;
import com.leoman.exception.ParamErrorException;
import com.leoman.service.CircleService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 草丛api
 * Created by 涂奕恒 on 2016/11/25 0018.
 */
@Controller
@RequestMapping(value = "api/circle")
public class CircleApi {

    @Autowired
    private CircleService circleService;

    /**
     * @api {post} /api/circle/list 1、草丛列表
     * @apiVersion 0.0.1
     * @apiName circle.list
     * @apiGroup circle
     *
     * @apiDescription 草丛列表
     *
     * @apiParam   {Integer}   userId                     用户Id
     * @apiParam   {Integer}   isMe                       是否是我发的，0=否，1=是，默认为0
     * @apiParam   {String}    tagName                    标签名称，中间用英文逗号隔开
     * @apiParam   {Integer}   pageNum                    页码
     * @apiParam   {Integer}   pageSize                   每页显示条数
     *
     * @apiSuccess {Object}    list
     * @apiSuccess {Integer}   list.id                    草丛ID
     * @apiSuccess {Integer}   list.title                 标题
     * @apiSuccess {String}    list.content               内容
     * @apiSuccess {String}    list.synopsis              简介
     * @apiSuccess {Object}    list.imageList             图片列表
     * @apiSuccess {String}    list.imageList.url         图片URL
     * @apiSuccess {String}    list.labels                标签名称，中间用英文逗号隔开
     * @apiSuccess {Integer}   list.isAdmin               是否管理员发布，0=否，1=是
     * @apiSuccess {Integer}   list.isTop                 是否置顶，0=否，1=是
     * @apiSuccess {Integer}   list.praiseNum             点赞数
     * @apiSuccess {Integer}   list.commentNum            评论数
     * @apiSuccess {Integer}   list.collectNum            收藏数
     * @apiSuccess {Integer}   list.isPraise              是否点赞，0=否，1=是
     * @apiSuccess {Integer}   list.isComment             是否评论，0=否，1=是
     * @apiSuccess {Integer}   list.userId                发送人ID
     * @apiSuccess {String}    list.nickName              发送人昵称
     * @apiSuccess {String}    list.avatar                发送人头像
     * @apiSuccess {String}    list.createTime            发送时间
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "page": {
     *              "totalNum": 0,
     *              "totalPage": 0,
     *              "currentPage": 1
     *          },
     *          "list": [
     *              {
     *                  "id" : 1,
     *                  "title" : 1,
     *                  "content" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草",
     *                  "synopsis" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草",
     *                  "imageList" : [
     *                      {
     *                          "url" : "http://www.qq.com"
     *                      }
     *                  ],
     *                  "labels" : "鲜花,户外,运动",
     *                  "isTop" : 0,
     *                  "isTop" : 1,
     *                  "praiseNum" : 124,
     *                  "commentNum" : 20,
     *                  "collectNum" : 20,
     *                  "isPraise" : 0,
     *                  "isComment" : 0,
     *                  "userId" : 1,
     *                  "nickName" : "张三",
     *                  "avatar" : "http://4.16.3.22:8080/images/cover.jpg",
     *                  "createTime" : "1476843673494"
     *              }
     *          ]
     *      }
     * }
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public void list(HttpServletResponse response, Integer userId, Integer isMe, String tagName, Integer pageNum, Integer pageSize) {
        Page<Circle> page = circleService.iPageForApp(userId, isMe, tagName, pageNum, pageSize);
        Map<String, Object> dataMap = JsonUtil.fitting(page);
        String result = JsonUtil.obj2ApiJson(Result.successApi(dataMap), "user", "cover1", "cover2", "cover3");
        WebUtil.printApi(response, result);
    }

    /**
     * @api {post} /api/circle/save 2、添加草丛
     * @apiVersion 0.0.1
     * @apiName circle.save
     * @apiGroup circle
     *
     * @apiDescription 添加草丛
     *
     * @apiParam   {Integer}   userId                用户Id                   （必填）
     * @apiParam   {Integer}   tempId                心事草稿Id
     * @apiParam   {Integer}   title                 标题
     * @apiParam   {String}    content               内容
     * @apiParam   {String}    synopsis              简介
     * @apiParam   {String}    labels                标签名称，中间用英文逗号隔开
     * @apiParam   {Stream}    image                 图片组
     * @apiParam   {Integer}   isDraft               是否草稿，0=否，1=是       （必填）
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{}
     * }
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(HttpServletResponse response, Integer tempId, Integer userId, String title, String content, String synopsis, String labels, Integer isDraft, MultipartRequest multipartRequest) {
        try {
            circleService.addCircleInfo(false, tempId, userId, title, content, synopsis, labels, isDraft, multipartRequest);
            String result = JsonUtil.obj2ApiJson(Result.successApi());
            WebUtil.printApi(response, result);
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_NOT_PARAM));
        }
    }

    /**
     * @api {post} /api/circle/draft 3、草丛草稿
     * @apiVersion 0.0.1
     * @apiName circle.draft
     * @apiGroup circle
     *
     * @apiDescription 草丛草稿
     *
     * @apiParam   {Integer}   userId                           用户Id               （必填）
     *
     * @apiSuccess {Object}    circleInfo
     * @apiSuccess {Integer}   circleInfo.id                    草丛草稿ID
     * @apiSuccess {Integer}   circleInfo.title                 标题
     * @apiSuccess {String}    circleInfo.content               内容
     * @apiSuccess {String}    circleInfo.synopsis              简介
     * @apiSuccess {String}    circleInfo.labels                标签名称，中间用英文逗号隔开
     * @apiSuccess {Object}    circleInfo.imageList             图片列表
     * @apiSuccess {String}    circleInfo.imageList.url         图片URL
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "circleInfo": {
     *              "id" : 1,
     *              "title" : "标题",
     *              "content" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草"
     *              "synopsis" : "我是一只萌萌哒小兔子，我生长在美丽的帕斯米尔大草原，我从小就喜欢吃青草"
     *              "labels" : "绿叶,暖日",
     *              "imageList" : [
     *                  {
     *                      "url" : "http://www.qq.com"
     *                  }
     *              ]
     *          }
     *      }
     * }
     */
    @RequestMapping(value = "/draft", method = RequestMethod.POST)
    public void draft(HttpServletResponse response, Integer userId) {
        try {
            Circle circle = circleService.iFindDraftByUserId(userId);
            String result = JsonUtil.obj2ApiJson(Result.successInfoApi(circle, "circleInfo"), "user", "isAdmin", "isTop", "praiseNum", "commentNum", "isPraise", "isCollect", "userId", "nickName", "avatar", "createTIme", "cover1", "cover2", "cover3");
            WebUtil.printApi(response, result);
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, JsonUtil.obj2ApiJson(Result.failureApi(ErrorType.ERROR_NOT_PARAM)));
        } catch (NoDataException e) {
            WebUtil.printApi(response, JsonUtil.obj2ApiJson(Result.failureApi(ErrorType.ERROR_NO_DATA)));
        }
    }
}
